package com.shg.admin.config;

import com.shg.admin.interceptor.LoginInterceptor;
import com.shg.admin.interceptor.RedisCountUrlInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;

/**
 * @DESCRIPTION:
 * @USER: shg
 * @DATE: 2023/11/28 0:19
 */
@Configuration
public class MyAdminWebConfig implements WebMvcConfigurer {
    
    private final RedisCountUrlInterceptor redisCountUrlInterceptor;

    public MyAdminWebConfig(RedisCountUrlInterceptor redisCountUrlInterceptor) {
        this.redisCountUrlInterceptor = redisCountUrlInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 需要放行的请求(即:不需要登录就可以访问的请求)
        List<String> excludePathPatterns = Arrays.asList(
                // 放行登录请求
                "/","/login",
                
                // 放行静态资源
                "/css/**",
                "/fonts/**",
                "/images/**",
                "/js/**",
                
                // 放行ico
                "/favicon.ico",
                
                // 以下是引入Swagger2需要放行的请求
                "/webjars/**",
                "/swagger-resources/**",
                "/swagger-ui.html",
                
                // SpringBoot默认处理错误的请求
                // "/error"
                
                // 放行监控功能
                "/actuator/**"
        );

        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**") // 拦截所有请求,包括静态资源
                .excludePathPatterns(excludePathPatterns);
        
        registry.addInterceptor(redisCountUrlInterceptor)
                .excludePathPatterns(excludePathPatterns);
    }
}
